from PIL import Image
import os

from settings import *
from analyze_image import calc_image
from turtle_recorder import Recorder
from draw_image import draw


def save_image(data):
	img = Image.fromarray(data)
	if not os.path.exists('output'):
		os.makedirs('output')
	if is_show:
		img.show()
	if is_save:
		output_name = f'output/output_{image_size[0]}x{image_size[1]}_{draw_mode}'
		if is_gray:
			output_name += f'_gray{precision}_sort'
		elif is_sort:
			output_name += f'_sort'
		if is_dpi:
			output_name += f'_{dpi[0]}dpi'
		output_name += f'_{filename}'
		if is_dpi:
			img.save(output_name, dpi=dpi)
		else:
			img.save(output_name)


def main(data):
	if is_draw:
		draw(data, draw_mode, image_size[0], image_size[1])


if __name__ == '__main__':
	image = Image.open('input/' + filename)
	img_type = 3
	if filename[-3:] == 'png':
		 img_type = 4
	width, height = image_size
	image = image.resize(image_size)
	data = calc_image(image, img_type, draw_mode, is_sort, is_gray, precision)
	save_image(data)
	main(data)

	# with Recorder(main, fps=30) as peg:
	#     main(data)
	print('程序完成')